package com.tpl.core.basedata.action.sys;

import java.util.List;

import com.tpl.core.action.BaseAction;
import com.tpl.core.basedata.common.constant.ICacheConstants;
import com.tpl.core.basedata.service.AuthService;
import com.tpl.core.basedata.service.MenuService;
import com.tpl.core.common.DateUtil;
import com.tpl.core.common.TreeHelper;
import com.tpl.core.common.ValidateUtil;
import com.tpl.core.common.redis.RedisClientUtil;
import com.tpl.core.entity.sys.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 说明 :菜单Action
 * @author <a href="mailto:3380084@gmail.com">张坤雷</a>
 * @version 创建时间：2013-7-3 下午4:01:21 
 *
 */
@Controller
@RequestMapping("/menu")
public class MenuAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8269863660035387371L;
	@Autowired
	private MenuService menuService;
	@Autowired
	private AuthService authService;

	@RequestMapping("/manage.do")
	public String menuManage(){
		return "menu/menu";
	}


	@RequestMapping("/getMenuTree.do")
	public @ResponseBody void getMenuTree(final Integer flag){
		List<Menu> list=(List<Menu>) authService.getMenu(ICacheConstants.KEY_AUTH_DEPT, getLoginUser());
		TreeHelper tree=new TreeHelper(new String[]{"id","menuName","menuNo","parentId","menuType","orderNum","menuUrl","lock","icon"},list,3) {
			@Override
			public Object root() {
				if(flag==null)
					return new Object[]{-1,"菜单管理"};
				switch(flag.intValue()){
					case 1:
						return -1;
					}
				return new Object[]{-1,"菜单管理"};
			}
			@Override
			public Object[] idTextData(Object[] nodeData) {
				return new Object[]{nodeData[0],nodeData[1]};
			}
			@Override
			protected Boolean beforeAddNode(Object[] nodeData) {
				if(flag==null||-1==Long.valueOf(nodeData[0].toString()))return null;
				switch(flag.intValue()){
				case 1:
					if("3".equals(nodeData[4]))return Boolean.FALSE;
					break;
				}
				return null;
			}
		};
		outJson(tree);
	}

	@RequestMapping("/saveOrUpdate.do")
	public @ResponseBody void saveOrUpdate(Menu menu){
		ValidateUtil.validLength(menu.getMenuName(), 2, 50,"菜单名称");
		ValidateUtil.validRequired(menu.getMenuNo(), "编码");
		ValidateUtil.validRequired(menu.getOrderNum(), "排序号");
		ValidateUtil.validRequired(menu.getParentId(), "上级菜单");
		menu.setUpdateDate(DateUtil.now());
		menu.setUpdator(getLoginUser().getUserNo());
		menuService.saveMenu(menu);
		RedisClientUtil.delkeyObject(ICacheConstants.CACHE_AUTH_ROLE_MENU);
		outJsonSuccess();
	}
	
	@RequestMapping("/del.do")
	public @ResponseBody void delete(Long id){
		menuService.deleteMenu(id);
		RedisClientUtil.delkeyObject(ICacheConstants.CACHE_AUTH_ROLE_MENU);
		outJsonSuccess();
	}
	@RequestMapping("/getMenu.do")
	public String getMenu(){
		return "menu/menuManage";
	}
	@RequestMapping("/editMenu.do")
	public String editMenu(){
		return "menu/menuEdit";
	}
}
